package com.xinxin.leetcode.problem168;

import java.util.Stack;

/**
 * 给定一个正整数，返回它在 Excel 表中相对应的列名称。
 * <p>
 * 例如，
 * <p>
 * 1 -> A
 * 2 -> B
 * 3 -> C
 * ...
 * 26 -> Z
 * 27 -> AA
 * 28 -> AB
 * ...
 * 示例 1:
 * <p>
 * 输入: 1
 * 输出: "A"
 * 示例 2:
 * <p>
 * 输入: 28
 * 输出: "AB"
 * 示例 3:
 * <p>
 * 输入: 701
 * 输出: "ZY"
 *
 * @author 史鑫鑫
 * @date 2019-08-26 2:50
 */
class Solution {
    public String convertToTitle(int n) {
        Stack<Character> stack = new Stack<>();
        while (n > 0) {
            stack.push((char) ('A' + ((n - 1) % 26)));
            n = (n - 1) / 26;
        }
        StringBuilder res = new StringBuilder();
        while (!stack.empty()) {
            res.append(stack.pop());
        }
        return res.toString();
    }

    public static void main(String[] args) {
        for (int i = 500; i < 720; i++) {
            System.out.println(i + ":" + new Solution().convertToTitle(i));
        }
    }
}
